1 00:00:00,170 --> 00:00:06,050 Alrighty, in this lecture, we're going to continue learning more about meta tables by using them to 2 00:00:06,050 --> 00:00:11,240 imitate a programming paradigm called object oriented programming. 3 00:00:11,240 --> 00:00:17,330 This lecture is just going to expose you to this new concept, as we take a much deeper dive into object 4 00:00:17,330 --> 00:00:19,640 oriented programming later in the course. 5 00:00:19,640 --> 00:00:24,320 So your first question is going to be what is object oriented programming? 6 00:00:24,320 --> 00:00:31,430 Well, object oriented programming is a programming paradigm that allows us to create objects. 7 00:00:31,430 --> 00:00:35,720 These objects hold their own unique properties and methods. 8 00:00:35,720 --> 00:00:42,650 And in fact, the same paradigm is what the Roblox game engine uses for all of the instances or objects 9 00:00:42,650 --> 00:00:43,850 in the game. 10 00:00:43,850 --> 00:00:51,050 For example, the base plate in the game is an object created by something called the part class, and 11 00:00:51,050 --> 00:00:52,430 we can see its type up here. 12 00:00:52,430 --> 00:00:53,810 As you can see, it says part. 13 00:00:53,810 --> 00:00:58,880 So this base plate is an object that belongs to the part class. 14 00:00:58,880 --> 00:01:02,570 And the part class gives our object a whole bunch of different properties. 15 00:01:02,570 --> 00:01:05,960 As you can see, all of these different properties belong to our part. 16 00:01:05,960 --> 00:01:12,680 And the properties window and our part also has some other events and functions or methods that belong 17 00:01:12,710 --> 00:01:14,870 to this particular object. 18 00:01:14,870 --> 00:01:22,490 Now a class can be thought of as a blueprint, and this blueprint tells us how we can create as many 19 00:01:22,490 --> 00:01:26,780 objects as we'd like that follow the standard of the blueprint. 20 00:01:26,780 --> 00:01:28,550 But there only exists one blueprint. 21 00:01:28,550 --> 00:01:31,940 For example, you can think of a blueprint for creating a car. 22 00:01:31,940 --> 00:01:36,230 That blueprint tells you exactly how you can create the car, but you can modify the car. 23 00:01:36,230 --> 00:01:38,750 Maybe you want to change the color, the engine. 24 00:01:38,750 --> 00:01:40,730 Maybe you want to add tint to the windows. 25 00:01:40,730 --> 00:01:47,540 There's ways for you to modify the object, but that one blueprint tells you the standard for creating 26 00:01:47,540 --> 00:01:48,170 a car. 27 00:01:48,170 --> 00:01:54,020 Now, Lua doesn't officially have a way to create classes, since Lua is considered to be a functional 28 00:01:54,020 --> 00:02:00,680 programming language, but we can use tables and meta tables in Lua to mimic or imitate classes. 29 00:02:00,680 --> 00:02:07,070 We can have a table act as our blueprint for creating and returning another table, and that table will 30 00:02:07,070 --> 00:02:08,570 act as the object. 31 00:02:08,570 --> 00:02:14,690 The table created by our class will have its own unique properties and methods, so it fulfills the 32 00:02:14,690 --> 00:02:16,520 role of being an object. 33 00:02:16,520 --> 00:02:21,560 Typically, these classes will have a function inside of them called a constructor. 34 00:02:21,560 --> 00:02:28,910 The purpose of this function, as the name suggests, is to construct new objects that inherit properties 35 00:02:28,910 --> 00:02:30,980 and functions from the class. 36 00:02:30,980 --> 00:02:37,460 In fact, virtually all of the instances and data types in the Roblox API are created through constructors, 37 00:02:37,460 --> 00:02:41,090 and these constructors are typically given the name of a dot new. 38 00:02:41,090 --> 00:02:44,690 And that's what we're also going to be naming our constructor in this lecture. 39 00:02:44,690 --> 00:02:47,600 So let's get started creating a new class. 40 00:02:47,600 --> 00:02:52,730 And the way I'm going to show you how to create a class is the standard way of doing it in Lua. 41 00:02:52,760 --> 00:02:58,370 Let's start off with a very simple example, and let's create a class for rectangles. 42 00:02:58,370 --> 00:03:02,930 So let's create a new table or a new variable that's going to store a table. 43 00:03:02,930 --> 00:03:05,420 And I'm going to call it rectangle. 44 00:03:05,630 --> 00:03:08,690 Now you're going to notice that I'm naming it in Pascal case. 45 00:03:08,690 --> 00:03:16,400 And typically you want to name any classes in Pascal case instead of naming it using the CamelCase naming 46 00:03:16,400 --> 00:03:17,000 convention. 47 00:03:17,000 --> 00:03:21,530 This way you can just differentiate your variables if it's going to be a class or if it's just, you 48 00:03:21,530 --> 00:03:24,320 know, a regular variable with this naming convention. 49 00:03:24,320 --> 00:03:27,770 But we're going to name it with the first letter being capitalized. 50 00:03:27,800 --> 00:03:32,630 Now let's go ahead and create a constructor function for our class. 51 00:03:32,630 --> 00:03:36,200 So this function is going to belong within this table. 52 00:03:36,200 --> 00:03:39,440 And the way we can do that is we can first type out the function keyword. 53 00:03:39,440 --> 00:03:44,360 We'll type out the name of the variable or the table which is going to be a rectangle. 54 00:03:44,360 --> 00:03:49,160 And then we'll use a dot to say, hey, we're going to be creating a new function in this table, and 55 00:03:49,160 --> 00:03:50,750 we're going to call it dot new. 56 00:03:50,750 --> 00:03:53,990 And then we can go ahead and add our parentheses and then hit enter. 57 00:03:53,990 --> 00:04:00,950 And here is our brand new constructor function that belongs inside of our rectangle table, which is 58 00:04:00,950 --> 00:04:02,660 going to be acting as our class. 59 00:04:02,660 --> 00:04:08,810 Now if we want to create a new rectangle we got to think what properties do rectangles have? 60 00:04:08,810 --> 00:04:12,320 Well, a rectangle can have a width and it can have a height. 61 00:04:12,320 --> 00:04:14,480 So let's define two parameters. 62 00:04:14,480 --> 00:04:18,740 One is going to be our width and one is going to be our height. 63 00:04:19,040 --> 00:04:23,180 Now our constructor has the responsibility of creating new objects. 64 00:04:23,180 --> 00:04:25,220 So let's create an object. 65 00:04:25,220 --> 00:04:27,680 Here we'll create a variable call it object. 66 00:04:27,680 --> 00:04:30,440 And that's going to be equal to a brand new table. 67 00:04:30,440 --> 00:04:35,690 So this table right here is going to act as the object inside of this table. 68 00:04:35,690 --> 00:04:38,330 We can go ahead and define some properties for it. 69 00:04:38,330 --> 00:04:45,440 For example the width which will set equal to the width passed to our constructor as well as the height. 70 00:04:45,440 --> 00:04:48,980 We're going to set that equal to the height passed to our constructor. 71 00:04:48,980 --> 00:04:52,910 Then we are able to return this object out of our constructor. 72 00:04:52,910 --> 00:04:53,690 And boom! 73 00:04:53,690 --> 00:04:56,360 Now we have a very basic working constructor. 74 00:04:56,360 --> 00:04:58,490 So let's actually try it out here real quick. 75 00:04:58,490 --> 00:04:59,420 Let's create. 76 00:04:59,790 --> 00:05:02,640 Two separate rectangle objects. 77 00:05:02,640 --> 00:05:05,040 And that's because these are going to be two different tables. 78 00:05:05,040 --> 00:05:07,950 So let's create a variable called rect one. 79 00:05:07,950 --> 00:05:09,450 So this is going to be our first rectangle. 80 00:05:09,450 --> 00:05:12,390 We could do rectangle dot new. 81 00:05:12,390 --> 00:05:15,240 And let's say the width and height of this one is going to be ten. 82 00:05:15,570 --> 00:05:18,330 And then let's do rect two. 83 00:05:18,330 --> 00:05:21,870 And that's going to also be equal to rectangle dot new. 84 00:05:22,230 --> 00:05:25,980 And actually let's also do the same height and width of ten. 85 00:05:25,980 --> 00:05:32,910 And to prove to you that these two tables are unique or basically these are two unique objects, let's 86 00:05:32,910 --> 00:05:38,700 go ahead and print to see if rectangle one is equal to rectangle two. 87 00:05:38,700 --> 00:05:44,640 So if we run this code, as you can see it's going to print false into the output. 88 00:05:44,640 --> 00:05:51,120 And that's because these are two different tables in memory even though they are storing the same values. 89 00:05:51,120 --> 00:05:57,420 So this allows us to maintain the uniqueness of our objects because these two objects aren't the same. 90 00:05:57,420 --> 00:05:57,810 All right. 91 00:05:57,810 --> 00:06:00,870 So our rectangle class is pretty basic right now. 92 00:06:00,870 --> 00:06:05,940 And the objects created by our class are really only storing data which is width and height. 93 00:06:05,940 --> 00:06:07,890 And we can't do much with this data. 94 00:06:07,890 --> 00:06:15,570 But now let's go ahead and add some functions to our class that are going to be methods for our objects. 95 00:06:15,570 --> 00:06:20,250 Now, if you want to create methods for your objects, you might think, okay, maybe we can define 96 00:06:20,250 --> 00:06:23,460 some functions for our object inside of the constructor, right? 97 00:06:23,460 --> 00:06:27,480 Maybe we want to have a function for our object. 98 00:06:27,480 --> 00:06:28,680 So actually let's do that real quick. 99 00:06:28,680 --> 00:06:33,630 Maybe we want to have a function for our object to get the area of the rectangle. 100 00:06:33,630 --> 00:06:34,770 And we could define that here. 101 00:06:34,770 --> 00:06:39,120 But there's actually a better way to define methods for our objects. 102 00:06:39,120 --> 00:06:43,350 And we don't have to do it every single time a new object is created. 103 00:06:43,350 --> 00:06:49,500 And the way that I'm going to show you how to do it also saves on memory, because every single object 104 00:06:49,500 --> 00:06:56,130 will refer to these functions, and they won't have to create a new function and store it in each object 105 00:06:56,130 --> 00:06:58,230 every single time we create a new rectangle. 106 00:06:58,230 --> 00:07:03,630 So instead what we could do is we could create these methods within our rectangle class. 107 00:07:03,630 --> 00:07:10,080 For example, we could create a get area function, and then we could have our objects be able to refer 108 00:07:10,080 --> 00:07:15,600 to these functions by utilizing the underscore underscore index meta method, which we talked about 109 00:07:15,600 --> 00:07:16,860 in the previous lecture. 110 00:07:16,860 --> 00:07:22,350 Right now, even though this function exists within our rectangle class, if we were to create a new 111 00:07:22,350 --> 00:07:26,100 rectangle object, we'll call this rect equal to rectangle dot new. 112 00:07:26,100 --> 00:07:28,470 We'll just do a width and height of one. 113 00:07:28,470 --> 00:07:34,920 If we try to refer to the function inside of our rectangle, as you can see, it only sees the width 114 00:07:34,920 --> 00:07:36,750 and the height which we created in the object. 115 00:07:36,750 --> 00:07:39,750 But it doesn't see this get area function. 116 00:07:39,750 --> 00:07:45,210 And that's because this rectangle object doesn't have a meta table attached to it with the underscore 117 00:07:45,210 --> 00:07:46,650 underscore index meta method. 118 00:07:46,650 --> 00:07:53,250 So what we can do is we could actually use our rectangle class as a meta table. 119 00:07:53,250 --> 00:08:00,870 So what we can do is we can create an underscore underscore index meta method inside of the rectangle 120 00:08:00,870 --> 00:08:01,530 table. 121 00:08:01,530 --> 00:08:04,320 And we want to set it equal to itself. 122 00:08:04,320 --> 00:08:08,040 So that way we can attach this meta table to our object. 123 00:08:08,040 --> 00:08:09,420 Let's do that right here. 124 00:08:09,420 --> 00:08:13,290 Instead of returning the object we're going to call the set meta table function. 125 00:08:13,290 --> 00:08:15,810 We're going to refer to our object. 126 00:08:15,810 --> 00:08:19,530 And we're going to attach the rectangle class to it. 127 00:08:19,530 --> 00:08:26,190 And now because this object has this rectangle class, what's going to happen is if we use the dot operator 128 00:08:26,190 --> 00:08:31,170 as you can see we're going to see a whole bunch of other things in here such as our get area function. 129 00:08:31,170 --> 00:08:36,540 And that's because if we go through the logic of the interpreter, when we try to index it with, for 130 00:08:36,540 --> 00:08:43,290 example, a key like get area, it's first going to check to see if this key exists within our object. 131 00:08:43,290 --> 00:08:46,920 Obviously our object doesn't have a key called get area. 132 00:08:46,920 --> 00:08:51,930 So the next thing it's going to do is it's going to check if our table has a meta table, which it does 133 00:08:51,930 --> 00:08:53,310 because we set it right here. 134 00:08:53,310 --> 00:08:58,860 Then it's going to check to see if that meta table has an underscore underscore index meta method which 135 00:08:58,890 --> 00:08:59,280 it does. 136 00:08:59,280 --> 00:09:00,600 We defined it right here. 137 00:09:00,600 --> 00:09:06,300 And then it's going to access the value inside of the meta method which is going to be either a function 138 00:09:06,300 --> 00:09:07,140 or a table. 139 00:09:07,140 --> 00:09:12,810 Since it's a table what it's going to do is it's going to check this table right here to see if it has 140 00:09:12,810 --> 00:09:14,400 that key we're looking for. 141 00:09:14,400 --> 00:09:15,360 And guess what? 142 00:09:15,360 --> 00:09:21,150 We declared a function inside of our rectangle class that has that exact same name. 143 00:09:21,150 --> 00:09:23,580 So it's going to use that as a function. 144 00:09:23,580 --> 00:09:29,190 And that's why we are able to see it with the intellisense and autofill it in our code. 145 00:09:29,190 --> 00:09:32,220 So let's go ahead and finish filling out our get area function. 146 00:09:32,220 --> 00:09:37,620 And all this needs to do is just return to us the value of the width multiplied by the height. 147 00:09:37,620 --> 00:09:39,660 That's how we get the area of our rectangle. 148 00:09:39,660 --> 00:09:45,570 Now an issue you're going to notice is wait, how do we know what the width and the height is of our 149 00:09:45,570 --> 00:09:47,370 object within this function? 150 00:09:47,370 --> 00:09:49,560 How do we how do we refer to our object? 151 00:09:49,560 --> 00:09:53,940 Maybe I guess we're just going to have to create a parameter right where we pass our object to it. 152 00:09:53,940 --> 00:09:57,780 And then we could do req to get area rect. 153 00:09:58,350 --> 00:09:59,070 But of course. 154 00:09:59,190 --> 00:09:59,880 This right here. 155 00:09:59,880 --> 00:10:01,320 Looks looks a little weird. 156 00:10:01,320 --> 00:10:07,440 Why do we have to pass wrecked to our function when we're calling the function from our rectangle object? 157 00:10:07,440 --> 00:10:15,270 Well, what we need to do is we need to replace declaring this function with a dot and use a colon. 158 00:10:15,270 --> 00:10:21,030 And now what has happened is that we're telling the interpreter that this function right here is a method 159 00:10:21,030 --> 00:10:26,670 or aka this function is going to belong to a table. 160 00:10:26,670 --> 00:10:33,300 And because we're doing this now, we have access to a hidden parameter in our function called self. 161 00:10:33,300 --> 00:10:35,340 As you can see, it pops up right here. 162 00:10:35,340 --> 00:10:41,790 This hidden parameter is important because like I mentioned, we need to be able to refer to the object 163 00:10:41,790 --> 00:10:44,460 that our function is being called upon. 164 00:10:44,460 --> 00:10:51,420 So you can think of self as a special variable that always points us to the object on which our method 165 00:10:51,420 --> 00:10:52,560 is being called. 166 00:10:52,560 --> 00:10:59,730 So this way, instead of having to pass our rectangle directly to our method, we can just simply use 167 00:10:59,730 --> 00:11:02,550 a colon and call our get area function. 168 00:11:02,550 --> 00:11:08,100 And now our object is going to be automatically passed to our get area method. 169 00:11:08,100 --> 00:11:12,480 And it's going to be stored inside of our hidden variable called self. 170 00:11:12,480 --> 00:11:18,090 So that means if I were to index self, we're going to be able to have access to the width and the height 171 00:11:18,090 --> 00:11:19,680 properties of our object. 172 00:11:19,680 --> 00:11:26,460 So calling our method this way is the exact same way of doing it like this where we use a dot instead 173 00:11:26,460 --> 00:11:29,940 and we pass the rectangle to the function doing it. 174 00:11:29,940 --> 00:11:32,430 These two ways are the exact same thing. 175 00:11:32,430 --> 00:11:33,630 It's just doing it this way. 176 00:11:33,630 --> 00:11:36,090 It's a lot easier and faster to type out. 177 00:11:36,090 --> 00:11:43,410 So now what we can do in our get area function is we can simply return the width of our object multiplied 178 00:11:43,410 --> 00:11:45,750 by the height of our object. 179 00:11:45,750 --> 00:11:49,950 And now let's go ahead and print that value out into the console. 180 00:11:50,130 --> 00:11:52,170 And then let's go ahead and change up these values. 181 00:11:52,170 --> 00:11:55,080 Let's make the width ten and the height ten. 182 00:11:55,080 --> 00:12:00,630 And then if we run this code as you can see we're going to get the value printed of 100 which is ten 183 00:12:00,630 --> 00:12:01,590 multiplied by ten. 184 00:12:01,590 --> 00:12:04,140 And that is the area of our rectangle. 185 00:12:04,140 --> 00:12:04,890 Look at that. 186 00:12:04,890 --> 00:12:05,820 It works. 187 00:12:05,820 --> 00:12:13,440 Now if we declared this method by using a dot and we called the function using a dot, what you're going 188 00:12:13,440 --> 00:12:18,510 to see is first off, the linter in the script editor is going to tell us that it doesn't know what 189 00:12:18,510 --> 00:12:19,170 self is. 190 00:12:19,170 --> 00:12:22,350 And that's because we didn't declare this function as a method. 191 00:12:22,350 --> 00:12:26,520 But if we go ahead and try to run this code, what you're going to see is we're going to run into an 192 00:12:26,520 --> 00:12:27,210 error. 193 00:12:27,210 --> 00:12:32,700 And the error says attempt to index nil with the width on line 14. 194 00:12:32,700 --> 00:12:36,150 So right here it says we tried to index nil with width. 195 00:12:36,150 --> 00:12:39,120 And that makes sense because self is going to be nil here. 196 00:12:39,120 --> 00:12:40,800 It doesn't know what self is. 197 00:12:40,800 --> 00:12:44,490 And we're not able to pass our object directly to our method. 198 00:12:44,490 --> 00:12:47,340 That's why we have to use a colon. 199 00:12:47,340 --> 00:12:52,620 And that's why when we call our function we need to use a colon as well. 200 00:12:52,620 --> 00:12:53,250 Okay. 201 00:12:53,250 --> 00:13:01,140 Let's create another method for our class and let's have this new method give us the parameter of our 202 00:13:01,140 --> 00:13:01,890 rectangle. 203 00:13:01,890 --> 00:13:04,980 So we can refer to our rectangle class. 204 00:13:04,980 --> 00:13:06,870 Create a new method. 205 00:13:06,870 --> 00:13:10,140 And this one we could call it get parameter. 206 00:13:10,410 --> 00:13:13,320 And we can basically do the exact same thing that we did up here. 207 00:13:13,320 --> 00:13:18,960 We're going to return self but we're going to do plus the height. 208 00:13:18,960 --> 00:13:21,660 And then we need to do this first. 209 00:13:21,660 --> 00:13:24,540 And then we can go ahead and multiply this by two. 210 00:13:25,190 --> 00:13:28,520 And this will give us the perimeter of our rectangle. 211 00:13:28,550 --> 00:13:32,810 So let's create another new rectangle using rectangle dot new. 212 00:13:32,840 --> 00:13:36,470 Let's make the width five and the height ten. 213 00:13:36,470 --> 00:13:41,210 And then let's go ahead and print the perimeter of our rectangle. 214 00:13:41,210 --> 00:13:47,090 So if we run this code as you can see we're going to get a value of 30 which is the perimeter for our 215 00:13:47,090 --> 00:13:47,840 rectangle. 216 00:13:47,840 --> 00:13:48,830 Very cool. 217 00:13:48,830 --> 00:13:51,830 So our class is coming together nicely. 218 00:13:51,830 --> 00:13:57,020 Now let's say we wanted to be able to add two rectangles together. 219 00:13:57,020 --> 00:14:02,210 For example let's say I had rectangle one here and then I had another rectangle here. 220 00:14:02,210 --> 00:14:04,460 We could call this rectangle dot new. 221 00:14:04,460 --> 00:14:06,080 And we'll do like ten and ten. 222 00:14:06,080 --> 00:14:08,870 And let's say I wanted to create a brand new rectangle. 223 00:14:08,870 --> 00:14:10,160 Let's call it rectangle three. 224 00:14:10,160 --> 00:14:15,320 And that's going to be equal to our first rectangle plus our second rectangle. 225 00:14:15,320 --> 00:14:20,210 Well, in order to do this operation right here we're going to have to add an underscore underscore 226 00:14:20,210 --> 00:14:23,510 add meta method into our rectangle class. 227 00:14:23,510 --> 00:14:29,570 So as a challenge for you, I would like for you to create a working underscore underscore add meta 228 00:14:29,570 --> 00:14:31,280 method to our class. 229 00:14:31,280 --> 00:14:33,590 And then you can go ahead and pause the lecture right now. 230 00:14:33,590 --> 00:14:34,820 Take a few minutes to do that. 231 00:14:34,820 --> 00:14:38,150 And then I will show you how I would approach it next. 232 00:14:38,390 --> 00:14:38,870 Okay. 233 00:14:38,870 --> 00:14:43,820 Let's go ahead and create an underscore underscore add meta method to our rectangle class. 234 00:14:43,820 --> 00:14:47,420 So we could do rectangle dot underscore underscore add. 235 00:14:47,420 --> 00:14:50,000 And we want to set that equal to a new function. 236 00:14:50,000 --> 00:14:53,450 And this function of course is going to be passed our first rectangle. 237 00:14:53,450 --> 00:14:54,530 We'll call it rect one. 238 00:14:54,530 --> 00:14:56,720 And it's going to be passed our second rectangle. 239 00:14:56,720 --> 00:14:58,400 We'll call that rect two. 240 00:14:58,730 --> 00:15:06,920 And then here we need to return a brand new rectangle object that let's say has the width of rect one 241 00:15:06,920 --> 00:15:11,090 and rect two added together, as well as both heights added together. 242 00:15:11,090 --> 00:15:17,690 And the easiest way to do that would be to just use our already pre-existing constructor, our new constructor, 243 00:15:17,690 --> 00:15:21,260 and all we need to do is just pass a new width and a new height. 244 00:15:21,260 --> 00:15:30,350 So we could do rectangle one dot width and then we can go ahead and add rectangles two width. 245 00:15:30,350 --> 00:15:36,980 And then for the height we can refer to rectangle one's height and add a rectangle two's height. 246 00:15:36,980 --> 00:15:42,620 And then we can just return this new object from our add meta method. 247 00:15:42,620 --> 00:15:44,210 And just like that we're done. 248 00:15:44,210 --> 00:15:48,980 So we can go ahead and print out rectangle three. 249 00:15:48,980 --> 00:15:51,560 Actually let's go ahead and print out all of these rectangles. 250 00:15:51,560 --> 00:15:53,210 So we'll print rectangle one. 251 00:15:53,210 --> 00:15:55,250 We'll print a rectangle two. 252 00:15:55,250 --> 00:15:57,560 And then we'll print rectangle three. 253 00:15:57,560 --> 00:16:00,650 And then let's copy this code and run it in the command line. 254 00:16:00,650 --> 00:16:01,370 There we go. 255 00:16:01,370 --> 00:16:03,050 We got three objects printed out. 256 00:16:03,050 --> 00:16:05,150 So let's go ahead and take a look at each of them. 257 00:16:05,300 --> 00:16:11,420 And as you can see here is our first object our first rectangle with the height of ten a width of five. 258 00:16:11,420 --> 00:16:13,940 Here's our second with a height and width of ten. 259 00:16:13,940 --> 00:16:20,480 And then here is our brand new rectangle that had both the height and the width of the first two rectangles 260 00:16:20,480 --> 00:16:21,650 added together. 261 00:16:21,650 --> 00:16:28,040 Now what if we would like to be able to multiply two rectangles together instead? 262 00:16:28,040 --> 00:16:34,070 Well, now we're going to need a meta method called underscore underscore mul short for multiply. 263 00:16:34,070 --> 00:16:41,030 So I would also like for you to attempt to create a working multiplication and meta method for our class, 264 00:16:41,030 --> 00:16:45,050 using the same logic that we used for our add meta method. 265 00:16:45,050 --> 00:16:46,040 So pause the lecture. 266 00:16:46,040 --> 00:16:50,120 Now take a few minutes and I'll show you my solution next okay. 267 00:16:50,120 --> 00:16:58,220 So let's go ahead and define inside of our rectangle class a new underscore underscore multiplication 268 00:16:58,220 --> 00:16:58,970 meta method. 269 00:16:58,970 --> 00:17:01,130 And we're going to set that equal to a function. 270 00:17:01,250 --> 00:17:05,120 And this function is also going to be passed our two rectangles. 271 00:17:05,120 --> 00:17:10,520 And then what we need to do is basically the exact same thing that we did for our addition meta method. 272 00:17:10,520 --> 00:17:16,220 But instead of adding the widths and the heights together, we can just simply multiply them together. 273 00:17:17,150 --> 00:17:17,870 And there we go. 274 00:17:17,870 --> 00:17:21,320 Just like that, we now have a meta method for multiplication. 275 00:17:21,590 --> 00:17:25,850 So if we were to run this brand new code, let's go ahead and see what outputs we get. 276 00:17:26,240 --> 00:17:29,000 There is our first two rectangle objects. 277 00:17:29,000 --> 00:17:31,190 And then our third one is look at that. 278 00:17:31,190 --> 00:17:34,490 We have our height of 110 multiplied by ten. 279 00:17:34,490 --> 00:17:38,390 And then we have our width of 50 which is ten multiply by five. 280 00:17:38,390 --> 00:17:41,750 So our multiplication meta method is working great. 281 00:17:41,750 --> 00:17:47,780 Now we could definitely keep adding more and more functionality to our class, and we could create more 282 00:17:47,780 --> 00:17:49,370 and more meta method definitions. 283 00:17:49,370 --> 00:17:55,250 But I think this is a pretty good spot to stop and a recap over what we just learned. 284 00:17:55,250 --> 00:18:00,620 So object oriented programming is a way for us to create objects from classes. 285 00:18:00,620 --> 00:18:07,310 Since Lua does not have a built in way to create classes, we have to imitate object oriented programming 286 00:18:07,310 --> 00:18:10,580 using tables and meta tables. 287 00:18:10,580 --> 00:18:10,940 Right? 288 00:18:10,940 --> 00:18:12,260 With our rectangle class. 289 00:18:12,260 --> 00:18:14,990 Here we have a function that belongs to the class. 290 00:18:14,990 --> 00:18:16,160 We called it dot new. 291 00:18:16,160 --> 00:18:17,780 And this is our constructor. 292 00:18:17,780 --> 00:18:22,490 And our constructor takes a width and a height for creating new objects. 293 00:18:22,490 --> 00:18:24,770 So we create a new object here which is just an. 294 00:18:24,900 --> 00:18:30,600 Empty table and we create two new properties inside of this object, which is the width and the height. 295 00:18:30,600 --> 00:18:34,770 And we set that equal to the width and the height passed to our constructor. 296 00:18:34,770 --> 00:18:41,310 Then afterwards we return our object with our rectangle class attached to it as the meta table. 297 00:18:41,310 --> 00:18:46,650 So that way our object has access to the methods such as get area and get perimeter. 298 00:18:46,650 --> 00:18:52,920 And it also has the functionality for being able to add rectangles together as well as multiply rectangles 299 00:18:52,920 --> 00:18:53,490 together. 300 00:18:53,490 --> 00:18:59,910 And the only reason why it's able to access these methods is because we have defined an underscore underscore 301 00:18:59,910 --> 00:19:03,360 index meta method that refers to the class itself. 302 00:19:03,360 --> 00:19:08,100 And then it will check inside of our class for these functions, which are right here. 303 00:19:08,100 --> 00:19:12,630 And then to finally demonstrate why this table right here is our class. 304 00:19:12,630 --> 00:19:17,400 Let's go ahead and just print out our rectangle table into the console. 305 00:19:18,780 --> 00:19:24,480 And if we take a look at it, as you can see it has our functions or methods of get area get perimeter. 306 00:19:24,480 --> 00:19:28,110 It has our meta methods of add index and multiply. 307 00:19:28,110 --> 00:19:32,010 And as you can see it also has our dot new constructor. 308 00:19:32,010 --> 00:19:36,090 So this table right here is acting as our class. 309 00:19:36,090 --> 00:19:36,810 All right. 310 00:19:36,810 --> 00:19:37,560 And guess what. 311 00:19:37,560 --> 00:19:40,020 That is all from me for this lecture. 312 00:19:40,020 --> 00:19:45,060 If this was way too much information for you to absorb right away, don't feel ashamed. 313 00:19:45,060 --> 00:19:48,540 Try to attempt the next quiz on classes and constructors. 314 00:19:48,540 --> 00:19:52,770 But if you fail the quiz, just continue moving on through the course and you can come back to this 315 00:19:52,770 --> 00:19:54,360 lecture at a later time. 316 00:19:54,360 --> 00:19:59,340 If you're feeling pretty confident with object oriented programming, then I would encourage you to 317 00:19:59,340 --> 00:20:05,910 continue adding more features to our rectangle class, or even create your own class from scratch. 318 00:20:05,910 --> 00:20:11,760 As I stated in the beginning, this lecture is mainly for just exposing you to this new concept, and 319 00:20:11,760 --> 00:20:15,600 we will touch more on object oriented programming later in the course. 320 00:20:15,600 --> 00:20:21,780 Otherwise, thanks for sticking through this heap of new knowledge and I will see you in the next lecture.